local kaiyun = fk.CreateSkill {
  name = "th_kaiyun",
  max_turn_use_time = 1,
}

kaiyun:addEffect(fk.AskForRetrial, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kaiyun.name) and data.card and not player:isKongcheng() and self:withinTimesLimit(player, Player.HistoryTurn)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local suits = {}
    local suitid = {}
    for _, cid in ipairs(player:getCardIds("h")) do
      local card = Fk:getCardById(cid)
      local suitname = Fk:translate(card:getSuitString()) .. Fk:translate(card:getSuitString(true))
      if not (suitname == "nosuit" or table.contains(suits, suitname)) then
        table.insert(suits, suitname)
        table.insert(suitid, Fk:getCardById(cid).suit)
      end
    end
    if #suits ~= 0 then
      table.insert(suits, "Cancel")
      local choice = room:askToChoice(player, {
        choices = suits,
        skill_name = kaiyun.name,
        prompt = "#th_kaiyun-ask::" .. target.id .. ":" .. data.reason,
        cancelable = true
      })
      if choice ~= "Cancel" then
        event:setCostData(self, suitid[table.indexOf(suits, choice)])
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = data.card ---@type Card
    local suit = event:getCostData(self)
    local discard = table.filter(player:getCardIds("h"), function(cid)
      return Fk:getCardById(cid).suit == suit
    end)
    room:throwCard(discard, kaiyun.name, player, player)
    player:drawCards(#discard, kaiyun.name)
    if suit == card.suit then
      player:drawCards(2, kaiyun.name)
    else
      room:setCardMark(card, "_th_kaiyun", suit)
      Fk:filterCard(card.id, data.who)
      data.card = room:filterCard(card.id, data.who, true)
      room:doBroadcastNotify("UpdateCard", card.id)
    end
  end,
}, { check_skill_limit = true })
kaiyun:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    for cid, cmark in pairs(player.room.card_marks) do
      if player.room:getCardArea(cid) == Card.DiscardPile then
        for name, _ in pairs(cmark) do
          if name == "_th_kaiyun" then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for cid, cmark in pairs(player.room.card_marks) do
      if player.room:getCardArea(cid) == Card.DiscardPile then
        for name, _ in pairs(cmark) do
          if name == "_th_kaiyun" then
            room:setCardMark(Fk:getCardById(cid), name, 0)
            Fk:filterCard(cid, room:getPlayerById(room.owner_map[cid]))
          end
        end
      end
    end
  end
})
kaiyun:addEffect("filter", {
  card_filter = function(self, card)
    return card and card:getMark("_th_kaiyun") ~= 0
  end,
  view_as = function(self, player, card)
    return Fk:cloneCard(card.name, card:getMark("_th_kaiyun"), card.number)
  end
})

Fk:loadTranslationTable{
  ["th_kaiyun"] = "开运",
  [":th_kaiyun"] = "每回合限一次，一名角色的判定牌生效前，你可以弃置一种花色的所有手牌并摸等量张牌，若此花色与判定牌花色相同，你摸两张牌，否则判定牌花色视为此花色直到进入弃牌堆。",

  ["#th_kaiyun-ask"] = "开运：请选择一种花色作为 %dest 的 %arg 判定牌的新花色（若相同则你摸两张牌），或点取消以取消发动此技能",
}

return kaiyun